package com.play.ballen.me.ui

import android.content.Context
import android.content.Intent
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.databinding.ActivityForgetPasswordBinding
import com.play.ballen.databinding.ActivityRegisterBinding
import com.play.ballen.me.vm.ForgetPwdVM
import com.play.ballen.me.vm.RegisterVM
import com.yuven.appframework.base.BaseStdTitleActivity
import com.yuven.appframework.util.setOnSingleClickListener
import com.yuven.baselib.utils.ResourceUtil

class ForgetPasswordActivity : BaseStdTitleActivity<ForgetPwdVM, ActivityForgetPasswordBinding>() {

    private var type: Int = TYPE_LOGIN_PWD

    companion object {

        const val TYPE_LOGIN_PWD = 0
        const val TYPE_PAY_PWD = 1

        @JvmStatic
        fun start(context: Context, type: Int) {
            val starter = Intent(context, ForgetPasswordActivity::class.java)
            starter.putExtra("type", type)
            context.startActivity(starter)
        }
    }

    private val countDownTip by lazy { ResourceUtil.getString(R.string.count_down_time_tip) }

    override fun viewModelClass(): Class<ForgetPwdVM> {
        return ForgetPwdVM::class.java
    }

    override fun contentLayoutId(): Int {
        return R.layout.activity_forget_password
    }

    override fun initObserver() {
        super.initObserver()

        viewModel.actionResetSuccess.observe(this) {
            ToastUtils.show(ResourceUtil.getString(R.string.forget_pwd_reset_ok))
            finish()
        }

        viewModel.actionCountDown.observe(this) {
            dataBinding.tvGetCode.isClickable = false
            dataBinding.tvGetCode.isEnabled = false
            dataBinding.tvGetCode.text = it.toString() + countDownTip
        }

        viewModel.actionCountDownComplete.observe(this) {
            dataBinding.tvGetCode.isClickable = true
            dataBinding.tvGetCode.isEnabled = true
            dataBinding.tvGetCode.text = ResourceUtil.getString(R.string.register_code_get)
        }
    }

    override fun initView() {
        super.initView()
        dataBinding.tvTitle.text =
            ResourceUtil.getString(if (type == TYPE_LOGIN_PWD) R.string.forget_title_login else R.string.forget_title_pay)
    }

    override fun initData() {
        dataBinding.tvGetCode.setOnSingleClickListener tag@{
            val account = dataBinding.edAccount.text.toString().trim()
            if (account.isEmpty()) {
                ToastUtils.show(dataBinding.edAccount.hint)
                return@tag
            }
            viewModel.sendCode(account)
        }

        dataBinding.btnResetPwd.setOnSingleClickListener tag@{
            val account = dataBinding.edAccount.text.toString().trim()
            if (account.isEmpty()) {
                ToastUtils.show(dataBinding.edAccount.hint)
                return@tag
            }

            val code = dataBinding.edCode.text.toString().trim()
            if (code.isEmpty()) {
                ToastUtils.show(dataBinding.edCode.hint)
                return@tag
            }

            val pwd = dataBinding.edPwd.text.toString().trim()
            if (pwd.isEmpty()) {
                ToastUtils.show(dataBinding.edPwd.hint)
                return@tag
            }

            val pwdConfirm = dataBinding.edPwdConfirm.text.toString().trim()
            if (pwdConfirm.isEmpty()) {
                ToastUtils.show(dataBinding.edPwdConfirm.hint)
                return@tag
            }

            if (pwdConfirm != pwd) {
                ToastUtils.show(ResourceUtil.getString(R.string.register_pwd_not_equal))
                return@tag
            }

            viewModel.forgetPwd(type, account, pwd, code)
        }
    }

    override fun initIntent(intent: Intent?) {
        intent?.let {
            type = it.getIntExtra("type", TYPE_LOGIN_PWD)
        }
    }

    override fun getToolBarTitle(): String = ""
}